package com.ea.admin.controller.tutelage;

import com.ea.admin.permission.PermissionCode;
import com.ea.admin.pojo.Enum.Permission;
import com.ea.admin.pojo.param.user.*;
import com.ea.admin.pojo.vo.user.UserSimpleInfoVo;
import com.ea.admin.pojo.vo.user.GetUserListVo;
import com.ea.admin.pojo.vo.user.ImportUserVo;
import com.ea.admin.service.UserService;
import com.ea.admin.utils.NowUserGetter;
import com.ea.common.log.LogAnnotation;
import com.ea.common.report.pojo.vo.ReportSimpleInfoListVo;
import com.ea.common.report.pojo.vo.UserDetailVo;
import com.ea.common.result.Result;
import com.ea.common.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "监护管理-人员管理")
@RequestMapping("/admin/user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    NowUserGetter nowUserGetter;
    @Resource
    HttpServletResponse response;

    @Transactional
    @Operation(summary = "获得人员名单")
    @PostMapping("/getUserList")
    @ApiImplicitParam(dataType = "GetUserListParam", name = "getUserListParam", value = "获得人员名单", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "获得人员名单")
    public Result<GetUserListVo> getUserList(@RequestBody @Valid GetUserListParam getUserListParam) {
        GetUserListVo result = userService.getUserList(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseIdList(getUserListParam.getCompanyIdList()),
                ParamUtil.parseIdList(getUserListParam.getDeptIdList()),
                ParamUtil.parseIdList(getUserListParam.getPositionIdList()),
                getUserListParam.getName(),
                getUserListParam.getPage(),
                getUserListParam.getPageSize()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "获得人员的详细信息")
    @PostMapping("/getUserDetail")
    @ApiImplicitParam(dataType = "GetUserDetailParam", name = "getUserDetailParam", value = "获得人员的详细信息", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "获得人员的详细信息")
    @Permission(permission_code = PermissionCode.USER_MANAGE)
    public Result<UserDetailVo> getUserDetail(@RequestBody @Valid GetUserDetailParam getUserDetailParam) {
        UserDetailVo result = userService.getUserDetail(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(getUserDetailParam.getUserId())
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "注册登记")
    @PostMapping("/addUser")
    @ApiImplicitParam(dataType = "AddUserParam", name = "addUserParam", value = "注册登记", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "注册登记")
    @Permission(permission_code = PermissionCode.ADD_USER)
    public Result<List<UserSimpleInfoVo>> addUser(@RequestBody @Valid AddUserParam addUserParam) {
        List<UserSimpleInfoVo> data=userService.addUser(
                nowUserGetter.getNowUser().getAccountId(),
                addUserParam.getAddUserItemParamList()
        );
        return Result.success(data);
    }


    @Transactional
    @Operation(summary = "外部系统添加用户")
    @PostMapping("/addUserFromSystem")
    @ApiImplicitParam(dataType = "AddUserFromSystemParam", name = "addUserFromSystemParam", value = "外部系统添加用户", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "外部系统添加用户")
    @Permission(permission_code = PermissionCode.ADD_USER)
    public Result<List<UserSimpleInfoVo>> addUserFromSystem(@RequestBody @Valid AddUserFromSystemParam addUserFromSystemParam) {
        List<UserSimpleInfoVo> data = userService.addUserFromSystem(
                nowUserGetter.getNowUser().getAccountId(),
                addUserFromSystemParam.getDataSourceToken(),
                addUserFromSystemParam.getAddUserItemParamList()
        );
        return Result.success(data);
    }

    @Transactional
    @Operation(summary = "添加临时人员")
    @PostMapping("/addTempUser")
    @ApiImplicitParam(dataType = "AddUserParam", name = "addUserParam", value = "注册登记", readOnly = true, required = true)
    public Result<List<UserSimpleInfoVo>> addTempUser(@RequestBody @Valid AddUserParam addUserParam) {
        List<UserSimpleInfoVo> data=userService.addTempUser(
                addUserParam.getAddUserItemParamList()
        );
        return Result.success(data);
    }

    @Transactional
    @Operation(summary = "修改人员信息")
    @PostMapping("/editUser")
    @ApiImplicitParam(dataType = "EditUserParam", name = "editUserParam", value = "修改人员信息", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "修改人员信息")
    @Permission(permission_code = PermissionCode.EDIT_USER,
            content = "修改人员“{userName}”的信息")
    public Result editUser(@RequestBody @Valid EditUserParam editUserParam) {
        userService.editUser(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(editUserParam.getUserId()),
                ParamUtil.parseId(editUserParam.getCompanyId()),
                ParamUtil.parseId(editUserParam.getDeptId()),
                ParamUtil.parseId(editUserParam.getPositionId()),
                editUserParam.getPhoto(),
                editUserParam.getName(),
                editUserParam.getSex(),
                editUserParam.getBirthday()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "删除人员")
    @PostMapping("/deleteUser")
    @ApiImplicitParam(dataType = "DeleteUserParam", name = "deleteUserParam", value = "删除人员", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "删除人员")
    @Permission(permission_code = PermissionCode.DELETE_USER,
            content = "删除人员“{userName}”")
    public Result deleteUser(@RequestBody @Valid DeleteUserParam deleteUserParam) {
        userService.deleteUser(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(deleteUserParam.getUserId())
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "下载批量导入的模板")
    @PostMapping("/downloadTemplate")
    @LogAnnotation(module = "UserController", operation = "下载批量导入的模板")
    public void downloadTemplate() {
        userService.downloadTemplate(response);
    }

    @Transactional
    @Operation(summary = "批量导入人员")
    @PostMapping("/importUser")
    @ApiImplicitParam(dataType = "ImportUserParam", name = "importUserParam", value = "批量导入人员", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "批量导入人员")
    @Permission(permission_code = PermissionCode.ADD_USER)
    public Result<ImportUserVo> importUser(@RequestBody @Valid ImportUserParam importUserParam) {
        ImportUserVo result = userService.importUser(
                nowUserGetter.getNowUser().getAccountId(),
                importUserParam.getExcelMd5(),
                importUserParam.getZipMd5());
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "导出人员列表")
    @PostMapping("/exportUser")
    @ApiImplicitParam(dataType = "ExportUserParam", name = "exportUserParam", value = "导出人员列表", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "导出人员列表")
    public void exportUser(@RequestBody @Valid ExportUserParam exportUserParam) {
        userService.exportUser(
                response,
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseIdList(exportUserParam.getCompanyIdList()),
                ParamUtil.parseIdList(exportUserParam.getDeptIdList()),
                ParamUtil.parseIdList(exportUserParam.getPositionIdList())
        );
    }

    @Transactional
    @Operation(summary = "获得人员的预警报告列表")
    @PostMapping("/getWarningReportList")
    @ApiImplicitParam(dataType = "GetWarningReportLust", name = "getWarningReportListParam", value = "获得人员的预警报告列表", readOnly = true, required = true)
    @LogAnnotation(module = "UserController", operation = "获得人员预警报告")
    public Result<ReportSimpleInfoListVo> getWarningReportList(@RequestBody @Valid GetWarningReportListParam getWarningReportListParam) {
        ReportSimpleInfoListVo result = userService.getWarningReportList(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(getWarningReportListParam.getUserId()),
                getWarningReportListParam.getWarningTypeList(),
                getWarningReportListParam.getPage(),
                getWarningReportListParam.getPageSize()
        );
        return Result.success(result);
    }


}
